/*
 * Copyright (C) 2012 DataSymphony (http://datasymphony.com.au)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package au.com.ds.restty.google;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.ds.restty.Client;


public class TokenCache {		
	private static final Logger LOG = LoggerFactory.getLogger(Client.class); 
	private static final String DETAILS_FILE_FORMAT = "%s/restty-oauth2_%s_auth.txt";

	public static class TokenDetails {		
		private String mTag;
		private String mAccessToken;
		private long mExpires;
		private String mRefreshToken;		
		
		private TokenDetails(String tag, String acceasToken, long expires, String refreshToken) {
			this.mTag = tag;
			this.mAccessToken = acceasToken;
			this.mExpires = expires;
			this.mRefreshToken = refreshToken;
		}

		public String getTag() {
			return mTag;
		}
		
		public String getAcceasToken() {
			return mAccessToken;
		}
		
		public long getExpires() {
			return mExpires;
		}
		
		public String getRefreshToken() {
			return mRefreshToken;
		}
		
		private static TokenDetails fromProperties(Properties props){
			String tag = props.getProperty("tag");
			String accessToken = props.getProperty("accessTokeng");
			long expires = Long.parseLong(props.getProperty("expires"));
			String refreshToken = props.getProperty("refreshToken");
			
			return new TokenDetails(tag, accessToken, expires, refreshToken);
		}
		
		private void toProperties(Properties props){
			props.put("tag", getTag());
			props.put("accessTokeng", getAcceasToken());
			props.put("expires", String.valueOf(getExpires()));
			props.put("refreshToken", getRefreshToken());
		}
	}

	private String mWorkDir;
	
	public TokenCache(String workDir){
		mWorkDir = workDir;
	}
	
	public String put(String tag, String acceasToken, long expires, String refreshToken){
		TokenDetails details = new TokenDetails(tag, acceasToken, expires, refreshToken);
		return writeTokenDetails(details);
	}
	
	public TokenDetails get(String tag){
		return readTokenDetails(tag);
	}
	
	private TokenDetails readTokenDetails(String tag){
		TokenDetails result = null;
		String fileName = String.format(DETAILS_FILE_FORMAT, mWorkDir, tag); 
		
		Properties props = new Properties();
		try {
			props.load(new FileReader(fileName));
			result = TokenDetails.fromProperties(props);
		} catch (Exception e) {
			LOG.warn("Error reading token file: " + fileName, e);
		}
		
		return result;
	}
	
	private String writeTokenDetails(TokenDetails details){
		Properties props = new Properties();
		String fileName = String.format(DETAILS_FILE_FORMAT, mWorkDir, details.getTag());
		
		try {
			details.toProperties(props);
			props.store(new FileWriter(fileName), "This file is managed by restty-oauth2");
		} catch (IOException e) {
			LOG.warn("Error writing token file: " + fileName, e);
		}
		
		return fileName;
	}
}
